/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.Message;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.entity.Product;
import com.hboxs.asl.entity.RecommendCommodity;
import com.hboxs.asl.service.ProductService;
import com.hboxs.asl.service.RecommendCommodityService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;

/**
 * Controller - 反馈信息
 */
@Controller("adminRecommendCommodityController")
@RequestMapping("/asl-admin/recommendCommodity")
public class RecommendCommodityController extends BaseController {

    @Resource(name = "recommendCommodityServiceImpl")
    private RecommendCommodityService recommendCommodityService;
    @Resource(name = "productServiceImpl")
    private ProductService productService;

    /**
     * 判断商品编号对应的商品是否存在
     *
     * @param sn
     * @return
     */
    @RequestMapping(value = "/isExisted", method = RequestMethod.GET)
    public
    @ResponseBody
    Boolean exist(String sn) {
        if (StringUtils.isEmpty(sn)) {
            return false;
        }
        return productService.findBySn(sn) != null;
    }

    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model) {
        return "/admin/recommendCommodity/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(RecommendCommodity recommendCommodity, String sn, RedirectAttributes redirectAttributes) {
        if (!isValid(recommendCommodity)) {
            return ERROR_VIEW;
        }

        Product product = productService.findBySn(sn);
        if(product==null){
            addFlashMessage(redirectAttributes, Message.error("找不到该商品"));
            return "redirect:add.htm";
        }

        //当 enable 字段为空时，表示启用，不为空时，表示启用
        recommendCommodity.setEnable(recommendCommodity.getEnable() != null);
        recommendCommodity.setProduct(product);
        if(recommendCommodityService.findByProduct(product)!=null){
            addFlashMessage(redirectAttributes, Message.error("该商品已经是推荐商品"));
            return "redirect:add.htm";
        }

        recommendCommodityService.save(recommendCommodity);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model) {
        model.addAttribute("recommendCommodity", recommendCommodityService.find(id));
        return "/admin/recommendCommodity/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(RecommendCommodity recommendCommodity, Long pid, RedirectAttributes redirectAttributes) {
        if (!isValid(recommendCommodity)) {
            return ERROR_VIEW;
        }
        recommendCommodity.setProduct(productService.find(pid));
        recommendCommodity.setEnable(recommendCommodity.getEnable() != null);
        recommendCommodityService.update(recommendCommodity, "product");
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, ModelMap model) {
        model.addAttribute("page", recommendCommodityService.findPage(pageable));
        return "/admin/recommendCommodity/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Message delete(Long[] ids) {
        recommendCommodityService.delete(ids);
        return SUCCESS_MESSAGE;
    }

}
